import requests
from tqdm import tqdm

# ohsome API endpoint for geometry data
url = "https://api.ohsome.org/v1/elements/geometry"

# Define the bounding polygon for the Black Sea region.
# Format: <customID>:<lon1>,<lat1>,<lon2>,<lat2>,...,<lonN>,<latN>
# Here, "BlackSea" is our custom ID and the coordinates form a rectangle.
bpolys_coords = "NewRegion:31.19966990896154,51.55212374515318,31.19966990896154,51.43526988997709,31.387118496582133,51.43526988997709,31.387118496582133,51.55212374515318,31.19966990896154,51.55212374515318"

# Set up the parameters:
# - time: snapshot date (November 2020)
# - filter: query features tagged as man_made=pipeline (i.e., all pipelines)
# - bpolys: our defined bounding polygon for the Black Sea region
params = {
    "time": "2021-11-01",
    "filter": "man_made=pipeline",
    "bpolys": bpolys_coords
}

# Make a streaming request to monitor download progress.
with requests.get(url, params=params, stream=True) as r:
    r.raise_for_status()  # Raises an error if the request failed.
    
    # Retrieve the total file size from headers if provided.
    total_size = int(r.headers.get('content-length', 0))
    
    # Open the output file in binary mode.
    with open("pipelines_blacksea_2020-11.json", "wb") as f, tqdm(
            total=total_size, unit='B', unit_scale=True, desc="Downloading"
    ) as progress_bar:
        # Download the data in chunks.
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:  # Filter out keep-alive chunks.
                f.write(chunk)
                progress_bar.update(len(chunk))

print("Download complete! The pipeline data has been saved to 'pipelines_blacksea_2020-11.json'.")
